﻿using System.Linq;
using VO;
using System.Collections.Generic;

namespace DAL
{
    public class StatusIncidenciaDAL
    {

        #region Insert
        public void Insert(StatusIncidencia obj)
        {
            var contexto = new CallcenterEntities();
            var objC = new tbCCStatusIncidencia();
            objC = PopulaObjeto(obj, objC);
            contexto.AddTotbCCStatusIncidencia(objC);
            contexto.SaveChanges();
        }
        #endregion

        #region Delete
        public void Delete(StatusIncidencia obj)
        {
            using (var contexto = new CallcenterEntities())
            {
                var incidencia = (from tb in contexto.tbCCStatusIncidencia
                                  where tb.idCampanha == obj.idCampanha &&  tb.idSituacao==obj.idSituacao && tb.Qtd==obj.qtd
                                  select tb);

                foreach (var objS in incidencia)
                {
                    contexto.DeleteObject(objS);
                }
                contexto.SaveChanges();
            }
        }
        #endregion

        #region Select
        public IList<StatusIncidencia> Select(StatusIncidencia obj)
        {
            var contexto = new CallcenterEntities();

            IQueryable<tbCCStatusIncidencia> status;

            status = (from statusAux in contexto.tbCCStatusIncidencia
                      where statusAux.idCampanha == obj.idCampanha &&
                      (statusAux.Qtd == obj.qtd || statusAux.Qtd == 0)
                      select statusAux);

            //Carrega lista
            var list = PopulaObjeto(status);

            return list;
        }

        #endregion

        #region PopulaObjeto
        protected static tbCCStatusIncidencia PopulaObjeto(StatusIncidencia obj, tbCCStatusIncidencia tabela)
        {
            tabela.idCampanha = obj.idCampanha;
            tabela.idSituacao = obj.idSituacao;
            tabela.Qtd = obj.qtd;
            tabela.Dias = obj.dias;
            tabela.fgBloqueio = obj.fgBloqueio;
            return tabela;
        }

        protected static IList<StatusIncidencia> PopulaObjeto(IQueryable<tbCCStatusIncidencia> reader)
        {
            IList<StatusIncidencia> list = new List<StatusIncidencia>();
            foreach (var obj in reader)
            {
                var newObj = new StatusIncidencia();
                newObj.idSituacao = obj.idSituacao;
                newObj.idCampanha = obj.idCampanha;
                newObj.qtd = obj.Qtd;
                newObj.dias = obj.Dias;
                newObj.idStatusIncidencia = obj.idStatusIncidencia;
                newObj.fgBloqueio = obj.fgBloqueio;
                list.Add(newObj);
            }
            return list;
        }
        #endregion

    }
}